package com.ykg.service.impl;

import com.ykg.dto.ListDTO;
import com.ykg.dto.NoticeDTO;
import com.ykg.mapper.ContractMapper;
import com.ykg.mapper.ListMapper;
import com.ykg.mapper.NoticeMapper;
import com.ykg.pojo.Contract;
import com.ykg.pojo.ListCharge;
import com.ykg.request.CheckNoticeRequest;
import com.ykg.pojo.NoticeLetter;
import com.ykg.service.INoticeService;
import com.ykg.util.CreateIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 类(接口)：
 * Created on 2021/6/23 15:59.
 * Author: zetong
 * Description:
 */
@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private DTOServiceImpl dtoService;

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ListMapper listMapper;

    @Override//查看通知单列表
    public List<NoticeLetter> getNoticeLetter(){
        List<NoticeLetter> noticeLetters = noticeMapper.findAll();
        return noticeLetters;
    }
    @Override//查看通知单详细信息
    public NoticeDTO noticeDetailed(String noticeId){
        NoticeLetter noticeLetter = noticeMapper.findNoticeById(noticeId);
        listMapper.updateListState(noticeLetter.getListId(),2);
        NoticeDTO noticeDTO = dtoService.createNoticeDto(noticeLetter);
        return noticeDTO;
    }
    @Override//审核通知单
    public int checkNotice(CheckNoticeRequest checkNoticeRequest){

        int state = 1;//通知单通过审核
        int result1 = noticeMapper.updateNoticeState(checkNoticeRequest.getNoticeId(),checkNoticeRequest.getReceiver(),new Date(),state,checkNoticeRequest.getRemark());

        CreateIdUtil createIdUtil= new CreateIdUtil();
        String contractId = createIdUtil.creatContractId();
        NoticeLetter noticeLetter = noticeMapper.findNoticeById(checkNoticeRequest.getNoticeId());
        long tid = 1;
        //生成待处理的合同
        Contract contract = new Contract(contractId,noticeLetter.getOwnerId(),noticeLetter.getListId(),noticeLetter.getShipId(),tid);
        int result2 = contractMapper.insertContract(contract);

        return result1+result2;
    }

}
